"""
多线程之间会共享全局变量
"""
import threading
import time

# 定义全局变量
g_num = 100
g_list = [1, 2]


def test1(args):
    """修改全局变量"""
    global g_num
    g_num += 1
    print("-----in thread1 g_num=%d------" % g_num)
    g_list.append(3)
    print("-----in thread1 g_list=%s------" % str(g_list))


def test2(args):
    print("-----in thread2 g_num=%d------" % g_num)
    print("-----in thread2 g_list=%s------" % str(g_list))


def main():
    # 线程传参使用args可选参数，该参数是元组类型
    t1 = threading.Thread(target=test1, args=(g_list,))
    t1.start()
    time.sleep(1)
    t2 = threading.Thread(target=test2, args=(g_list,))
    t2.start()
    time.sleep(1)
    print("-----in main g_num=%d------" % g_num)
    print("-----in thread2 g_list=%s------" % str(g_list))


if __name__ == '__main__':
    main()
